package com.rc.evcharger.common.terminal;

import java.util.ArrayList;
import java.util.List;


public class HearBeat implements Result {

    private String gunNum; // 1.枪号
    private String connStatus; //1.插枪，2.未插枪
    private String gunStatus;// 根据枪数解析（长度动态变化），1、空闲,2、充电，3、故障 4、枪锁故障。
    private String fault; // 故障码，待补充
    private String version; //电桩程序的版本号

    public HearBeat() {
        super();
    }

    public HearBeat(String gunNum, String connStatus, String gunStatus, String fault) {
        super();
        this.gunNum = gunNum;
        this.connStatus = connStatus;
        this.gunStatus = gunStatus;
        this.fault = fault;
    }

    public String getGunNum() {
        return gunNum;
    }

    public void setGunNum(String gunNum) {
        this.gunNum = gunNum;
    }

    public String getConnStatus() {
        return connStatus;
    }

    public void setConnStatus(String connStatus) {
        this.connStatus = connStatus;
    }

    public String getGunStatus() {
        return gunStatus;
    }

    public void setGunStatus(String gunStatus) {
        this.gunStatus = gunStatus;
    }

    public String getFault() {
        return fault;
    }

    public void setFault(String fault) {
        this.fault = fault;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    @Override
    public List<HearBeat> getResult(String dataStr) {
        List<HearBeat> result = new ArrayList<>();
        String data = dataStr.trim();
        // 获取枪数量;
        int gunNum = Integer.valueOf(data.substring(0, 2));
        data = data.substring(3);
        for (int i = 0; i < gunNum; i++) {
            // 枪号
            String gunOrder = String.valueOf(i + 1);
            // 获取连接状态
            String connStatus = data.substring(0, 2);
            data = data.substring(3);
            // 获取枪状态
            String gunStatus = data.substring(0, 2);
            data = data.substring(3);
            // 获取故障代码;
            String fault = data.substring(0, 5);
            data = data.substring(6);
            result.add(new HearBeat(gunOrder, connStatus, gunStatus, fault));

        }

        //获取电桩的版本号
        String version = data.substring(0, 5);
        version = versionToInt(version);

        for (HearBeat hearBeats : result) {
            hearBeats.setVersion(version);
        }
        return result;
    }

    private String versionToInt(String version) {
        String[] v = version.split(" ");
        version = v[1] + v[0];
        return Integer.parseInt(version, 16) + "";
    }

    public static void main(String[] args) {
        HearBeat hearBeats = new HearBeat();
        List<HearBeat> list = hearBeats.getResult("01 02 01 00 00 00 00 00 00");
        System.out.println(1);
    }
}
